﻿using System;
using System.Drawing;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 快速切换弹幕模式
{
    class GDIMessage
    {
        #region 用户变量
        private System.Windows.Forms.Timer timer;
        private System.Windows.Forms.Form f;
        private Pen p1;
        XeControl xe;
        #endregion
        public GDIMessage()
        {
            timer = new System.Windows.Forms.Timer();
            timer.Interval = 5000;
            timer.Tick += Timer_Tick;
            p1 = new Pen(Color.Black, 2.0F);
            newWindow();
        }
        public void show(string msg)
        {
            xe.Text = msg;
            animaxOp(f);
            drawContant(f);
            timer.Start();
        }
        /// <summary>
        /// 画图
        /// </summary>
        /// <param name="f"></param>
        private void drawContant(System.Windows.Forms.Form f)
        {
            
        }

        private void newWindow()
        {
            f = new System.Windows.Forms.Form();
            f.ShowInTaskbar = false;
            f.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            f.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            f.Width = 200;
            f.Height = 50;
            f.Opacity = 0.0;
            f.BackColor = Color.White;
            xe = new XeControl();
            xe.Top = 0;xe.Left = 0;
            f.Controls.Add(xe);
            f.Show();
        }
        /// <summary>
        /// 淡入淡出
        /// </summary>
        /// <param name="f"></param>
        /// <param name="reverse"></param>
        public void animaxOp(System.Windows.Forms.Form f,bool reverse = false)
        {
            Task.Run(new Action(()=>
            {
                while (true)
                {
                    switch (reverse)
                    {
                        case true:
                            f.Opacity -= 0.01;
                            if (f.Opacity<0.02)
                            {
                                if (reverse)
                                {
                                    f.Close();
                                }
                                return;
                            }
                            break;
                        case false:
                            f.Opacity += 0.01;
                            if (f.Opacity>0.7)
                            {
                                return;
                            }
                            break;
                    }
                    Thread.Sleep(10);
                }

            }));
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            animaxOp(f, true);
            timer.Stop();
        }
    }
}
